package com.cinsea.schedule.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.directory.model.Directory;
import com.cinsea.directory.service.DirectoryService;
import com.cinsea.form.model.Form;
import com.cinsea.form.model.Formrelationship;
import com.cinsea.form.service.FormService;
import com.cinsea.form.service.FormfieldService;
import com.cinsea.form.service.FormrelationshipService;
import com.cinsea.hrm.model.Human;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.htmlselect.model.Option;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.schedule.model.Schedulesetting;
import com.cinsea.schedule.service.SchedulesettingService;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;
import com.cinsea.workflow.model.Wfdefine;
import com.cinsea.workflow.service.WfdefineService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

public class ScheduleAction extends ExtAction {
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.trimToNull(request.getParameter("action"));
        if (action.equals("getfullschedule")) {
            DirectoryService directoryService = Env.getBean("directoryService");
            WfdefineService wfdefineService = Env.getBean("wfdefineService");
            FormrelationshipService formrelationshipService = Env.getBean("formrelationshipService");
            FormService formService = Env.getBean("formService");
            SchedulesettingService schedulesettingService = Env.getBean("schedulesettingService");
            FormfieldService formfieldService = Env.getBean("formfieldService");
            HumanService humanService = Env.getBean("humanService");
            OptionService optionService = Env.getBean("optionService");
            JdbcDao jdbcDao = Env.getBean("jdbcDao");
            String isone = StringUtil.null2String(request.getParameter("isone"));
            String ismy = StringUtil.null2String(request.getParameter("ismy"));
            String humanid = StringUtil.null2String(request.getParameter("humanid"));
            String resourceid = StringUtil.null2String(request.getParameter("resourceid"));
            String view = StringUtil.null2String(request.getParameter("view"));
            String startparam = DateUtil.formatDate(new Date(Long.parseLong(StringUtil.null2String(request.getParameter("start"))) * 1000));
            String endparam = DateUtil.formatDate(new Date(Long.parseLong(StringUtil.null2String(request.getParameter("end"))) * 1000));

            String oneself = StringUtil.null2String(request.getParameter("oneself"));
            String share = StringUtil.null2String(request.getParameter("share"));
            String sqlwhere = "";
            Wfdefine wf = new Wfdefine();
            Directory directory = new Directory();
            String formid = StringUtil.null2String(request.getParameter("formid"));
            if (StringUtil.isEmpty(formid)) {
                String directoryid = StringUtil.null2String(request.getParameter("directoryid"));
                String workflowid = StringUtil.null2String(request.getParameter("workflowid"));
                if (StringUtil.isEmpty(directoryid)) {
                    sqlwhere = " and processid in(select id from cs_wfprocess where deleted=0)";
                    wf = wfdefineService.get(workflowid);
                    formid = wf.getFormid();
                } else {
                    sqlwhere = " and processid in(select id from cs_formprocess where deleted=0)";
                    directory = directoryService.getDirectoryById(directoryid);
                    formid = directory.getPFormid();
                }
            }
            Form form = formService.getFormById(formid);
            int formtype = form.getObjtype().intValue();
            String hsql;
            if (formtype == 0)
                hsql = "from Schedulesetting where formid='" + formid + "'";
            else {
                String formids = "";
                String strHql = "from Formrelationship where hostid='" + formid + "' order by slaveid";
                List list = formrelationshipService.findFormlink(strHql);
                for (int i = 0; i < list.size(); i++) {
                    Formrelationship formlink = (Formrelationship) list.get(i);
                    String pid = formlink.getSlaveid();
                    if (formids.equals(""))
                        formids += pid;
                    else
                        formids += "','" + pid;
                }
                hsql = "from Schedulesetting where formid in('" + formids + "')";
            }
            List list1 = schedulesettingService.find(hsql);
            Schedulesetting schedulesetting = new Schedulesetting();
            for (int i = 0; i < list1.size(); i++) {
                schedulesetting = (Schedulesetting) list1.get(0);
                form = formService.getFormById(schedulesetting.getFormid());
                break;
            }
            String resources = "";
            String begindate = "";
            String enddate = "";
            String title = "";
            String content = "";
            String color = "";
            String begintime = "";
            String endtime = "";
            String creator = "";
            if (!StringUtil.isEmpty(schedulesetting.getId())) {
                resources = formfieldService.getFormfieldById(schedulesetting.getResourcefield()).getFieldname();
                begindate = formfieldService.getFormfieldById(schedulesetting.getBegindatefield()).getFieldname();
                enddate = formfieldService.getFormfieldById(schedulesetting.getEnddatefield()).getFieldname();
                begintime = formfieldService.getFormfieldById(schedulesetting.getBegintimefield()).getFieldname();
                endtime = formfieldService.getFormfieldById(schedulesetting.getEndtimefield()).getFieldname();
                content = formfieldService.getFormfieldById(schedulesetting.getContentfield()).getFieldname();
                color = formfieldService.getFormfieldById(schedulesetting.getColorfield()).getFieldname();
                title = formfieldService.getFormfieldById(schedulesetting.getTitlefield()).getFieldname();
                creator = formfieldService.getFormfieldById(schedulesetting.getCreatorfield()).getFieldname();
            } else {
                resources = "resources";
                begindate = "BeginDate";
                enddate = "EndDate";
                title = "Title";
                color = "color";
                content = "content";
                color = "begintime";
                content = "endtime";
                creator = "creator";
            }
            String sql = "";
            List<Map> list = new ArrayList();
            if (!StringUtil.isEmpty(humanid)) {
                if (StringUtil.isEmpty(share)) {
                    if (!StringUtil.isEmpty(isone) || !StringUtil.isEmpty(ismy)) {
                        if (StringUtil.isEmpty(oneself)) {
                            sql = "select * from " + form.getTablename() + " where deleted=0 and " + resources + " like '%" + humanid + "%' and  " + begindate + " <= ? and " + enddate + ">=?  " + sqlwhere;

                        } else {//自己参与 vs 自己创建
                            if (oneself.equals("1")) {
                                sql = "select * from " + form.getTablename() + " where deleted=0 and " + resources + " like '%" + humanid + "%' and  " + begindate + " <= ? and " + enddate + ">=?  and " + creator + "='" + humanid + "'" + sqlwhere;

                            } else {
                                sql = "select * from " + form.getTablename() + " where deleted=0 and " + resources + " like '%" + humanid + "%' and  " + begindate + " <= ? and " + enddate + ">=? and " + creator + "!='" + humanid + "'" + sqlwhere;

                            }
                        }


                    }
                } else {
                    if (share.equals("1")) {
                        sql = "select * from " + form.getTablename() + " where  deleted=0 and " + resources + " not like '%" + humanid + "%' and  " + begindate + " <= ? and " + enddate + ">=? " + sqlwhere;
                        sql = JdbcUtil.getPermissionSql(sql, form.getTablename());
                    }
                }
            } else {
                if (StringUtil.isEmpty(resourceid))

                    sql = "select * from " + form.getTablename() + " where deleted=0 and " + begindate + " <= ? and " + enddate + ">=? "; //资源管理
                else
                    sql = "select * from " + form.getTablename() + " where deleted=0 and " + resources + " like '%" + resourceid + "%' and " + begindate + " <= ? and " + enddate + ">=? " + sqlwhere;
            }


            list = jdbcDao.getJdbcTemplate().queryForList(sql, new Object[]{endparam, startparam});


            List array = new ArrayList();
            for (Object o : list) {
                Map object = new HashMap();
                String title1 = ((Map) o).get(title) == null ? "" : ((Map) o).get(title).toString();
                object.put("title", "查看");
                String BeginDate = ((Map) o).get(begindate) == null ? "" : ((Map) o).get(begindate).toString();

                String begintime1 = ((Map) o).get(begintime) == null ? "" : " " + ((Map) o).get(begintime).toString();

                String begin = BeginDate + begintime1;
                object.put("start", begin);
                String enddate1 = ((Map) o).get(enddate) == null ? "" : ((Map) o).get(enddate).toString();

                String endtime1 = ((Map) o).get(endtime) == null ? "" : " " + ((Map) o).get(endtime).toString();
                String end = enddate1 + endtime1;
                object.put("end", end);
                String color1 = ((Map) o).get(color) == null ? "" : ((Map) o).get(color).toString();
                Option selectitemcolor = optionService.get(color1);
                object.put("color", selectitemcolor.getObjdesc());
                String strname = "";
                if (!StringUtil.isEmpty(humanid)) {
                    String resources1 = ((Map) o).get(resources) == null ? "" : ((Map) o).get(resources).toString();
                    List resourceslist = StringUtil.string2ArrayList(resources1, ",");
                    for (int i = 0; i < resourceslist.size(); i++) {
                        Human human = humanService.getHumanById(resourceslist.get(i).toString());
                        if (i == 0) {
                            if (human != null)
                                strname = StringUtil.null2String(human.getObjname());
                            else strname = "";
                        } else {
                            if (human != null)
                                strname = "," + StringUtil.null2String(human.getObjname());
                        }
                    }
                } else {
                    strname = "";

                }

                object.put("resources", strname);
                String creatorname = "";
                if (!StringUtil.isEmpty(creator)) {
                    String creatorid = ((Map) o).get(creator) == null ? "" : ((Map) o).get(creator).toString();
                    if (!creatorid.equals("")) {
                        Human human = humanService.getHumanById(creatorid);
                        if (human != null)
                            creatorname = StringUtil.null2String(human.getObjname());
                    }

                }

                object.put("creator", creatorname);
                String content1 = ((Map) o).get(content) == null ? "" : ((Map) o).get(content).toString();
                content1 = content1.replace("\"", "");
                object.put("description", title1);

                String processid = ((Map) o).get("processid") == null ? "" : ((Map) o).get("processid").toString();
                object.put("id", processid);
                array.add(object);
            }
            response.getWriter().print(JSON.toJSONString(array));
            return;
        }

    }
}
